package org.example.algorithm.dichotomy;

//示例 1：
//
//输入：nums = [3,4,5,1,2]
//输出：1
//解释：原数组为 [1,2,3,4,5] ，旋转 3 次得到输入数组。
public class FindMinSolution {

    public int findMin2(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return -1;
        }
        if (len == 1) {
            return nums[0];
        }
        int left = 0;
        int right = len-1;
        int min = Integer.MAX_VALUE;
        while (left <= right) {
            int middle = (left + right) >> 1;
            if (nums[middle] < nums[right]) {
                min = Math.min(min, nums[middle]);
                right = middle;
            } else {
                min = Math.min(min, nums[right]);
                left = middle + 1;
            }
        }
        return min;
    }

    //简洁版
    public int findMin(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left < right) {
            int middle = (left + right) >> 1;
            if (nums[middle] > nums[right]) {
                left = middle + 1;
            } else {
                right = middle;
            }
        }
        return nums[left];
    }
}
